package com.wx.mall.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.goods.dto.StockDto;
import com.wx.mall.goods.entity.Stock;
import com.wx.mall.goods.mapper.StockMapper;
import com.wx.mall.goods.service.IStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.goods.vo.req.StockReqVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-14
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {

    @Override
    public List<Stock> findAll(StockDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("stock_num").eq("discrepancy",0).eq("stock_delete",1);
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("residue_stock").eq("discrepancy",0).eq("stock_delete",1);
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("creation_time").eq("discrepancy",0).eq("stock_delete",1);
        };
        List<Stock> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<Stock> findScreen(StockDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<Stock> wrapper=new QueryWrapper<>();
        if (dto.getName()!=null){
            wrapper.like("name",dto.getName()).eq("discrepancy",0).eq("stock_delete",1);
        }
        if (dto.getSpuNum()!=null){
            wrapper.like("spu_num",dto.getSpuNum()).eq("discrepancy",0).eq("stock_delete",1);
        }
        if (dto.getOrderId()!=null){
            wrapper.like("order_id",dto.getOrderId()).eq("discrepancy",0).eq("stock_delete",1);
        }
        if (dto.getStockType()!=null){
            wrapper.like("stock_type",dto.getStockType()).eq("discrepancy",0).eq("stock_delete",1);
        }
        if (dto.getOperationType()!=null){
            wrapper.eq("operation_type",dto.getOperationType()).eq("discrepancy",0).eq("stock_delete",1);
        }
        if (dto.getAdminId()!=null){
            wrapper.like("admin_id",dto.getAdminId()).eq("discrepancy",0).eq("stock_delete",1);
        }
        if (dto.getCreationTime()!=null){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(dto.getCreationTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date date=calendar.getTime();
            System.out.println(date);
            wrapper.le("creation_time",date).eq("discrepancy",0).eq("stock_delete",1);
        }
        if (dto.getSort()==0){
            wrapper.orderByAsc("stock_num").eq("discrepancy",0).eq("stock_delete",1);
        };
        if (dto.getSort()==1){
            wrapper.orderByAsc("residue_stock").eq("discrepancy",0).eq("stock_delete",1);
        };
        if (dto.getSort()==2){
            wrapper.orderByAsc("creation_time").eq("discrepancy",0).eq("stock_delete",1);
        };
        List<Stock> list1=this.list(wrapper);
        return list1;
    }

    @Override
    public int deleteList(StockReqVo vo) {
        List<Stock> list=new ArrayList<>();
        for (Long id:vo.getIds()
             ) {
            Stock stock=this.getById(id);
            stock.setStockDelete(0);
            list.add(stock);
        };
        boolean stocks = this.updateBatchById(list);
        System.out.println(stocks);
        return stocks?1:0;
    }

    @Override
    public List<Stock> findTwo(StockDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("stock_num").eq("discrepancy",1).eq("stock_delete",1);
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("residue_stock").eq("discrepancy",1).eq("stock_delete",1);
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("creation_time").eq("discrepancy",1).eq("stock_delete",1);
        };
        List<Stock> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<Stock> findScreenTwo(StockDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<Stock> wrapper=new QueryWrapper<>();
        if (dto.getName()!=null){
            wrapper.like("name",dto.getName()).eq("discrepancy",1).eq("stock_delete",1);
        }
        if (dto.getSpuNum()!=null){
            wrapper.like("spu_num",dto.getSpuNum()).eq("discrepancy",1).eq("stock_delete",1);
        }
        if (dto.getOrderId()!=null){
            wrapper.like("order_id",dto.getOrderId()).eq("discrepancy",1).eq("stock_delete",1);
        }
        if (dto.getStockType()!=null){
            wrapper.like("stock_type",dto.getStockType()).eq("discrepancy",1).eq("stock_delete",1);
        }
        if (dto.getOperationType()!=null){
            wrapper.eq("operation_type",dto.getOperationType()).eq("discrepancy",1).eq("stock_delete",1);
        }
        if (dto.getAdminId()!=null){
            wrapper.like("admin_id",dto.getAdminId()).eq("discrepancy",1).eq("stock_delete",1);
        }
        if (dto.getCreationTime()!=null){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(dto.getCreationTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date date=calendar.getTime();
            System.out.println(date);
            wrapper.le("creation_time",date).eq("discrepancy",1).eq("stock_delete",1);
        }
        if (dto.getSort()==0){
            wrapper.orderByAsc("stock_num").eq("discrepancy",1).eq("stock_delete",1);
        };
        if (dto.getSort()==1){
            wrapper.orderByAsc("residue_stock").eq("discrepancy",1).eq("stock_delete",1);
        };
        if (dto.getSort()==2){
            wrapper.orderByAsc("creation_time").eq("discrepancy",1).eq("stock_delete",1);
        };
        List<Stock> list1=this.list(wrapper);
        return list1;
    }


}
